home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / misc / emu / prlink_080b.lha / prlink-0.8.0b / src / prload.c < prev    next >
C/C++ Source or Header  |  1995-04-06  |  5KB  |  218 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include "prtrans.h"
  5.  
  6. #if !defined(__MAIN_C__)
  7. #define main    main_prload
  8. #endif
  9.  
  10. int main (int argc, char **argv);
  11.  
  12. unsigned char buffer[65536];
  13.  
  14. #define RUN_RUN        1
  15. #define RUN_JUMP    2
  16. #define RUN_LOADBASIC    4
  17. #define RUN_LOADADDR    8
  18.  
  19. int main (int argc, char **argv) {
  20.   unsigned runflag = 0, jumpaddress = 0, basicaddr, driveraddr;
  21.   FILE *file;
  22.   unsigned bank = 0, start, given_start = 0;
  23.   unsigned long length;
  24.   char **parameters = argv;
  25.   baseaddr = portaddr[DEFAULT_PORT];
  26.  
  27.   while (*++parameters && **parameters == '-') { /* check for options */
  28.     if (parameters[0][1] == '-') { /* "--" ends options */
  29.       parameters++;
  30.       break;
  31.     }
  32.  
  33.     switch (parameters[0][1]) {
  34.     case 'a':
  35.       given_start = strtoul (*++parameters, NULL, 16);
  36.       if (given_start >= 65536)
  37.     goto Usage;
  38.       runflag = (runflag & ~(RUN_LOADBASIC)) | RUN_LOADADDR;
  39.  
  40.       break;
  41.  
  42.     case 'b':
  43.       bank = strtoul (*++parameters, NULL, 16);
  44.  
  45.       if (bank > 255) {
  46.         fprintf (stderr, "%s: Illegal memory bank specified.\n", *argv);
  47.         return 1;
  48.       }
  49.  
  50.       break;
  51.  
  52.     case 'p':
  53.       baseaddr = strtoul (*++parameters, NULL, 16);
  54.  
  55.       if (baseaddr > 3) {
  56.         fprintf (stderr, "%s: The printer port number must be between 0 and 3.\n",
  57.                          *argv);
  58.         return 1;
  59.       }
  60.  
  61.       baseaddr = portaddr[baseaddr];
  62.  
  63.       break;
  64.  
  65.     case 'r':
  66.       runflag = (runflag & ~(RUN_JUMP)) | RUN_RUN;
  67.       break;
  68.  
  69.     case 'l':
  70.       runflag = (runflag & ~(RUN_LOADADDR)) | RUN_LOADBASIC;
  71.       break;
  72.  
  73.     case 'j':
  74.       if (*++parameters) {
  75.         runflag = (runflag & ~(RUN_RUN)) | RUN_JUMP;
  76.         jumpaddress = strtoul (*parameters, NULL, 16);
  77.  
  78.     if (jumpaddress < 65536)
  79.           break;
  80.       }
  81.       /* bleed through */
  82.  
  83.     case '?':
  84.     case 'h':
  85.     Usage:
  86.       fprintf (stderr, "%s: Uploads files "
  87.                        "to a memory area on a remote computer.\n\n", *argv);
  88.       fprintf (stderr, "Usage: %s [options] filename(s)\n",
  89.                        *argv);
  90.       fprintf (stderr, "Options:\n\t"
  91.              "-a addr\t"         
  92.                  "Override the load address from the file\n\t"
  93.                          "-b bank\t"
  94.                              "Specify the memory bank (in hex)\n\t"
  95.              "-l\t"
  96.                  "Load the file as BASIC.\n\t"
  97.              "-r\t"
  98.                  "Performs the BASIC RUN command after the transfers.\n\t"
  99.                          "-j addr\t"
  100.                  "Jumps to the specified hex address after the transfers.\n\t"
  101.                          "-p port\t"
  102.                              "Specify the printer port (0 to 3)\n");
  103.       return 1;
  104.  
  105.     default:
  106.       fprintf (stderr, "%s: Illegal option `%s'.\n", *argv, *parameters);
  107.       goto Usage;
  108.     }
  109.   }
  110.  
  111.   if (prinit ()) {
  112.     fprintf (stderr, "%s: Could not get the I/O permissions.\n", *argv);
  113.     return 4;
  114.   }
  115.  
  116.   if (!*parameters) /* no file name specified */
  117.     goto Usage;
  118.  
  119.   while (*parameters) {
  120.     if (!(file = fopen(*parameters, "rb"))) {
  121.       fprintf (stderr, "%s: Could not open the file `%s'.\n", *argv,
  122.                        *parameters);
  123.       return 5;
  124.     }
  125.  
  126.     if (fseek (file, 0, SEEK_END)) {
  127.       fprintf (stderr, "%s: Could not seek to the end of the file `%s'.\n",
  128.                        *argv, *parameters);
  129.       fclose (file);
  130.       return 5;
  131.     }
  132.  
  133.     length = ftell (file);
  134.  
  135.     if (length < 3) {
  136.       fprintf (stderr, "%s: File `%s' is shorter than 1 byte.\n", *argv,
  137.                        *parameters);
  138.       fclose (file);
  139.       return 5;
  140.     }
  141.  
  142.     length -= 2;
  143.  
  144.     fseek (file, 0, SEEK_SET);
  145.  
  146.     if (length > 65536) {
  147.       fprintf (stderr, "%s: File `%s' is longer than 64 kilobytes.\n",
  148.                        *argv, *parameters);
  149.       fclose (file);
  150.       return 5;
  151.     }
  152.  
  153.     start = fgetc (file);
  154.     start |= (fgetc (file) << 8);
  155.  
  156.     if (length > fread (buffer, 1, length, file)) {
  157.       fprintf (stderr, "%s: Reading the file `%s' failed.\n",
  158.                *argv, *parameters);
  159.       fclose (file);
  160.       return 5;
  161.     }
  162.  
  163.     output (REQ_INFO); /* request machine type info */
  164.  
  165.     fprintf (stderr, "%s: Preparing to upload `%s' to machine type %u.\n",
  166.                      *argv, *parameters, wait_input ());
  167.  
  168.     driveraddr = input();
  169.     driveraddr |= input() << 8;
  170.     basicaddr = input();
  171.     basicaddr |= input() << 8;
  172.  
  173.     fprintf (stderr, "%s: Driver address %04X, BASIC start address %04X, "
  174.                      "original load address %04X.\n",
  175.                      *argv, driveraddr, basicaddr, start);
  176.  
  177.     if (runflag & RUN_LOADBASIC)
  178.       start = basicaddr;
  179.     if (runflag & RUN_LOADADDR)
  180.       start = given_start;
  181.  
  182.     output (REQ_LOAD); /* op code for upload */
  183.  
  184.     output (bank); /* specify the bank address */
  185.  
  186.     if (input ()) {
  187.       fprintf (stderr, "%s: not ready to transfer\n", *argv);
  188.       fclose (file);
  189.       return 6;
  190.     }
  191.     else
  192.       fprintf (stderr, "%s: ok to transfer\n", *argv);
  193.  
  194.     output (start);
  195.     output (start >> 8);
  196.     output (start + length);
  197.     output ((start + length) >> 8);
  198.  
  199.     send (buffer, length);
  200.  
  201.     fclose (file);
  202.     parameters++;
  203.   }
  204.  
  205.   if (runflag & 1)
  206.     output (REQ_RUN); /* tell the server to shut off and to run the code */
  207.   else if (runflag == 2) { /* jump to a program address */
  208.     output (REQ_JUMP);
  209.     output (0);       /* the bank address */
  210.     output (jumpaddress);
  211.     output (jumpaddress >> 8);
  212.   }
  213.  
  214.   prclose ();
  215.  
  216.   return 0;
  217. }
  218.